package com.spa.application.service.admin;

import cn.hutool.core.bean.BeanUtil;
import com.spa.application.command.admin.AdminCarouselAddCommand;
import com.spa.application.command.admin.AdminCarouselEditCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.AdminCarouselDTO;
import com.spa.application.query.BaseIdQuery;
import com.spa.application.query.admin.AdminCarouselQuery;
import com.spa.domain.entity.Carousel;
import com.spa.domain.service.CarouselService;
import com.spa.infrastructure.enums.dict.carousel.CarouselTypeEnum;
import com.spa.infrastructure.exception.SpaException;
import com.spa.infrastructure.mapper.CarouselServiceMapperStruct;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description: 轮播图
 *
 * @author: lig
 * @date: 2024/3/4
 */
@Service
@Slf4j
public class AdminCarouselApplicationService {

    @Resource
    private CarouselService carouselService;

    /**
     * @description: 添加轮播图
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public void add(AdminCarouselAddCommand command) {
        if(ObjectUtils.isEmpty(CarouselTypeEnum.byCode(command.getType()))){
            throw new SpaException("类型不存在");
        }
        if(!carouselService.add(CarouselServiceMapperStruct.INSTANCE.commandAdmin2Entity(command))){
            throw new SpaException("添加失败");
        }
    }

    /**
     * @description: 修改轮播图
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public void edit(AdminCarouselEditCommand command) {
        if(StringUtils.isNotEmpty(command.getImgKey()) && command.getImgKey().length() > 40){
            command.setImgKey(null);
        }
        Carousel carousel = carouselService.infoById(command.getId());
        if(BeanUtil.isNotEmpty(carousel)){
            carouselService.edit(CarouselServiceMapperStruct.INSTANCE.commandAdmin2Entity(command));
        }else {
            throw new SpaException("未找到要修改的对象");
        }
    }

    /**
     * @description: 分页轮播图
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public PageResult<AdminCarouselDTO> page(AdminCarouselQuery query) {
        return CarouselServiceMapperStruct.INSTANCE.entity2Dto4Page(carouselService.page(CarouselServiceMapperStruct.INSTANCE.queryAdmin2Carousel(query)));
    }

    /**
     * @description: 上下架轮播图
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public void onOffShow(BaseIdQuery baseIdQuery){
        if(!carouselService.onOffShow(baseIdQuery.getId())) {
            throw new SpaException("操作失败");
        }
    }

    /**
     * @description: 删除轮播图
     *
     * @author: lig
     * @date: 2024/3/4
     */
    public boolean delById(BaseIdQuery baseIdQuery){
        return carouselService.delById(baseIdQuery.getId());
    }
}
